package com.zdxlz.fcmp.upms.admin;

import com.zdxlz.fcmp.common.core.constant.enums.OperateTypeEnum;
import com.zdxlz.fcmp.common.core.util.R;
import com.zdxlz.fcmp.common.log.annotation.LogRecord;
import com.zdxlz.fcmp.common.security.service.SysLoginService;
import com.zdxlz.fcmp.common.security.util.UserLoginInfoUtil;
import com.zdxlz.fcmp.upms.api.service.ISystemCommonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static com.zdxlz.fcmp.common.core.constant.CommonConstants.UPDATE_PASSWORD_SUCCESS;

/**
 * @author: aman
 * @className SystemCommonController
 * @date 2025/2/5 10:45
 * @description
 */

@RestController
@Tag(name = "公共接口模块")
@RequestMapping("/api/v1/sys/comm")
@AllArgsConstructor
public class SystemCommonController {

    @Resource
    private SysLoginService sysLoginService;

    @Resource
    private ISystemCommonService systemCommonService;

    @GetMapping("/info")
    @Operation(summary = "获取系统名称")
    @LogRecord(module = "获取系统名称", operType = OperateTypeEnum.SELECT)
    public R getSystemInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("systemName", "量子能力管理平台");
        return R.ok(map);
    }

    @GetMapping("/user/info")
    @Operation(summary = "获取当前登陆用户信息")
    @LogRecord(module = "获取当前登陆用户信息", operType = OperateTypeEnum.SELECT)
    public R getUserInfo() {
        return R.ok(UserLoginInfoUtil.getUserLoginDetail());
    }

    /**
     * @description：获取当前用户有效的角色信息，返回List<Map>集合，新增用户使用
     * @author：aman
     * @date：2025/2/25 17:03
     */
    @GetMapping("/user/role")
    @Operation(summary = "获取当前用户有效的角色信息")
    @LogRecord(module = "获取当前用户有效的角色信息", operType = OperateTypeEnum.SELECT)
    public R getUserRoleInfo() {
        return R.ok(systemCommonService.getUserRoleInfo(UserLoginInfoUtil.getUserInfo()));
    }

    /**
     * @description：获取所有的租户信息，返回List<Map>集合，新增用户使用
     * @author：aman
     * @date：2025/2/25 17:03
     */
    @GetMapping("/user/tenant")
    @Operation(summary = "获取所有的租户信息")
    @LogRecord(module = "获取所有的租户信息", operType = OperateTypeEnum.SELECT)
    public R getUserLinkTenantInfo() {
        return R.ok(systemCommonService.getUserLinkTenantInfo());
    }

    /**
     * @description：获取当前登陆人有效且有权限得的菜单信息，返回List<>集合，新增角色使用
     * @author：aman
     * @date：2025/2/25 17:03
     */
    @GetMapping("/user/menu")
    @Operation(summary = "获取当前登陆人有效且有权限得的菜单信息")
    @LogRecord(module = "获取当前登陆人有效且有权限得的菜单信息", operType = OperateTypeEnum.SELECT)
    public R getUserMenuInfo() {
        return R.ok(systemCommonService.getUserMenuInfo(UserLoginInfoUtil.getUserInfo()));
    }

    /**
     * @description：通过当前用户登陆账号，修改当前用户密码
     * @author：aman
     * @date：2025/2/25 17:03
     */
    @PostMapping("/reset/password")
    @Operation(summary = "重置当前登陆人的密码")
    @LogRecord(module = "重置当前登陆人的密码", db = true, operType = OperateTypeEnum.UPDATE)
    public R updateUserPassword(@RequestHeader("Authorization") String authorization, @RequestBody Map<String, String> map) {
        return R.ok(sysLoginService.updateUserPassword(authorization, UserLoginInfoUtil.getUserLoginAccount(), map), UPDATE_PASSWORD_SUCCESS);
    }
}
